// 导入必要的模块
import { defineStore } from 'pinia'
import { ref, onMounted } from 'vue'

// 定义主题存储
export const useThemeStore = defineStore('theme', () => {
  // 响应式状态
  const isDarkTheme = ref(false);

  // 切换主题
  const toggleTheme = () => {
    isDarkTheme.value = !isDarkTheme.value;
    document.documentElement.classList.toggle('dark', isDarkTheme.value);
  };

  // 应用主题
  const applyTheme = () => {
    document.documentElement.classList.toggle('dark', isDarkTheme.value);
  };

  // 组件挂载后应用保存的主题
  onMounted(() => {
    applyTheme();
  });

  // 导出状态和方法
  return {
    isDarkTheme,
    toggleTheme,
    applyTheme
  };
}, {
  // 启用持久化存储
  persist: true
});